const app =getApp()
const xAudio = wx.createInnerAudioContext({
  obeyMuteSwitch: false,
  success: function (res) {
      console.log(res)
      console.log("开启静音模式下播放音乐的功能");
  },
  fail: function (err) {
      console.log(err)
      console.log("静音设置失败");
  },
});
/* 背景音乐 */
const xBackMusic = wx.getBackgroundAudioManager();
const videoContext = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList:[],
    list:[],
    navcurrent:0,
    current:-1,// 当前播放音频
    current_video:-1,// 当前播放视频
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that =this
    that.xInit()
  },
  onShow() {

  },
  onReady(){
    xBackMusic.src='1'
  },
  xInit:function(){
    let that =this
    // 获取分类
    wx.cloud.callFunction({
      name: 'relaxation_mindfulness',
      data: {
        action: "get_classification"
      }
    }).then(res => {
      that.setData({
        navList:res.result
      })
      if(res.result.length==0){
        that.getList('')
      }else{
        that.getList(res.result[0]._id)
      }
    })
  },
  // 根据分类id获取音视频
  getList:function(id){
    let that =this
    wx.cloud.callFunction({
      name: 'relaxation_mindfulness',
      data: {
        action: "get_content_by_classification",
        classification_id:id
      }
    }).then(res => {
        that.setData({
          list:res.result,
          loading:false
        })
    })
  },
  // 切换分类
  changeType:function(e){
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    this.setData({
      navcurrent: index
    })
    this.getList(id)
  },
    // 音频播放
  xToPlay:function(e){
    let that =this
    let params = e.currentTarget.dataset
    let index = params.index
    // 未购买且是付费内容 提示购买
    if(params.pay == true && !app.globalData.product_buy){
      wx.showModal({
        title: "提示",
        content: "解锁Pro版",
        confirmText:'去解锁',
        success (res) {
          if (res.confirm) {
            that.xToProductde()
          }
        }
      })
      return
    }
    // 非首个播放 暂停上一个播放视频
    if(that.data.current_video!=-1){
      this.videoContext.stop()
      that.setData({
        [`list[${that.data.current_video}].play`]:false
      })
    }
    // 非首个播放 暂停上一个音频
    if(that.data.current !=-1){
      this.xAudioStop(this.data.current)
      that.setData({
        [`list[${that.data.current}].play`]:false
      })
    }
    // 根据类型 设置音视频播放 1音频 2视频
    if(params.type==1){
      if(xBackMusic.src!=params.audio){
        // xAudio.src = params.audio
        xBackMusic.src = params.audio
        xBackMusic.title = params.title
        if(that.data.current != -1){
          that.setData({
            [`list[${that.data.current}].play`]:false
          })
        }
      }
      that.setData({
        current:index,
        [`list[${index}].play`]:true
      })
      xBackMusic.play()
      // that.xAudioPlay(index)
    }else{
      if(that.data.current_video!=index){
        if(that.data.current_video != -1){
          that.videoContext.stop()
          that.setData({
            [`list[${that.data.current_video}].play`]:false
          })
        }
      }
      that.setData({
        current_video:index,
        [`list[${index}].play`]:true
      })
      // 视频播放时直接设置为全屏播放
      this.videoContext = wx.createVideoContext(index+'video')
      this.videoContext.requestFullScreen({ direction: 0 })
      this.videoContext.play()
    }
    
  },
  // 音视频暂停
  xTostop:function(e){
    let index = e.currentTarget.dataset.index
    // 当前播放按钮设置为false
    this.setData({
      [`list[${index}].play`]:false
    })
    // 根据类型 暂停音频或视频播放
    if(e.currentTarget.dataset.type==1){
      this.xAudioStop(index)
    }else{
      this.videoContext.stop()
    }
  },
  xAudioStop:function(){
    xBackMusic.stop()
  },
  // 产品详情
  xToProductde:function(){
    let that =this
    if(!app.globalData.user.phone_number){
      let path = '/pages/navigation/using/index'
      wx.navigateTo({
        url: '/pages/navigation/login/index?path='+path,
      })
    }else{
      wx.navigateTo({
        url: '/subsale/pages/product/index/index?choose=true&id=26da8e4962b93a6000ec7c3d68d9d3eb'
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})